package com.zixuan.gulicollege.uservice.controller;


import com.zixuan.gulicollege.common_utils.JwtUtils;
import com.zixuan.gulicollege.common_utils.R;
import com.zixuan.gulicollege.entity.ucenter.UcenterMember;
import com.zixuan.gulicollege.entity.ucenter.vo.RegisterVO;
import com.zixuan.gulicollege.uservice.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zixuan
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/u_service/ucenter")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @PostMapping("/loginUser")
    public R loginUser(@RequestBody UcenterMember user) {
        String token = ucenterMemberService.loginUser(user);
        return R.ok().data("token", token);
    }

    /**
     * 注册用户
     *
     * @param registerVO
     * @return
     */
    @PostMapping("/registerUser")
    public R registerUser(@RequestBody RegisterVO registerVO) {
        ucenterMemberService.registerUser(registerVO);
        return R.ok();
    }

    /**
     * 获取token值
     *
     * @param request
     * @return
     */
    @GetMapping("/getMemberTokenInfo")
    public R getMemberInfo(HttpServletRequest request) {
        // 根据request获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        // 查询数据库id获取用户信息
        UcenterMember userTokenInfo = ucenterMemberService.getById(memberId);
        return R.ok().data("userTokenInfo", userTokenInfo);
    }
}

